//
//  MySettingViewController.m
//  SsehMaster
//
//  Created by yang yang on 2018/8/15.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "MySettingViewController.h"
#import <SDImageCache.h>
#import "LogInViewController.h"

@interface MySettingViewController ()

@property (weak, nonatomic) IBOutlet UILabel *memoryLabel;
@property (weak, nonatomic) IBOutlet UIButton *logotButton;

@property (weak, nonatomic) IBOutlet UIView *bgView;


@end

@implementation MySettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    self.view.backgroundColor = [UIColor backGroudColorForView];
    self.logotButton.layer.borderColor = [UIColor roseRed].CGColor;
    self.logotButton.layer.borderWidth = 1.0f;
    self.logotButton.layer.cornerRadius = 5;
    
    //获取缓存图片的大小(字节)
    NSUInteger bytesCache = [[SDImageCache sharedImageCache] getSize];
    //换算成 MB (注意iOS中的字节之间的换算是1000不是1024)
    float imageCache = bytesCache/1000/1000;
    self.memoryLabel.text = [NSString stringWithFormat:@"%.2fMB",imageCache];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.bgView addGestureRecognizer:tap];
    
}

-(void)tapClick{
    
    dispatch_queue_t aDQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, aDQueue, ^{
        [[SDImageCache sharedImageCache] clearMemory];
    });
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
    self.memoryLabel.text = @"0.0MB";
}

- (IBAction)logoutClick:(id)sender {
    
    UIAlertAction * act1 = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction * act2 = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self logoutApp];
    }];
    UIAlertController * aleVC = [UIAlertController alertControllerWithTitle:nil message:@"确定退出吗？" preferredStyle:UIAlertControllerStyleAlert];
    [aleVC addAction:act1];
    [aleVC addAction:act2];
    [self presentViewController:aleVC animated:YES completion:nil];
    
    
}

-(void)logoutApp{
    //kOutLogin
    [self showLoading];
    NSDictionary * par = @{@"member_id":[NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(SsehUserID)],@"member_token":[NSString emptyStr:DEF_PERSISTENT_GET_OBJECT(SsehUserToken)]};
    [[QYNetManager sharedManager]POST:kOutLogin parameters:par andApiTyp:SsehApiTypMain SuccessBlock:^(id responseObject) {
        [self hideLoading];
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            [self removeUserInfo];
            self.logotButton.userInteractionEnabled = NO;
            [self performSelector:@selector(backLoginVC) withObject:nil afterDelay:1.0f];
        }else{
            [self showErrorTipWithTitle:responseObject[@"error"]];
        }
    } Failure:^(NSError *error) {
        [self hideLoading];
        [self showErrorTipWithTitle:SsehPoolNetStr];
    }];
    
}

-(void)removeUserInfo{
    
    
    
}

-(void)backLoginVC{
    LogInViewController * regis = [[LogInViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:regis];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    nav.navigationBar.titleTextAttributes = dict;
    nav.navigationBar.barTintColor = [UIColor appColor];
    self.view.window.rootViewController = nav;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
